package com.lsh.airticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dao.AirportDao;
import com.lsh.airticket.dao.AirrouteDao;
import com.lsh.airticket.dao.CityDao;
import com.lsh.airticket.dto.RequestAirroute;
import com.lsh.airticket.dto.ShowAirroute;
import com.lsh.airticket.entity.Airport;
import com.lsh.airticket.entity.Airroute;
import com.lsh.airticket.service.AirrouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author lsh
 * @date 2021/2/17
 **/
@Service
public class AirrouteServiceImpl implements AirrouteService {

    @Autowired
    AirrouteDao airrouteDao;

    @Autowired
    AirportDao airportDao;

    @Autowired
    CityDao cityDao;

    @Override
    public PageInfo<ShowAirroute> findAirrouteList() {

        ArrayList<ShowAirroute> showAirroutes = new ArrayList<>();

        List<Airroute> tbAirrouteAll = airrouteDao.findTbAirrouteAll();
        for (Airroute airroute : tbAirrouteAll) {
            ShowAirroute showAirroute = new ShowAirroute();
            //查找 起始城市 和终点城市
            String fromCity = airportDao.findCityName((int) (long) airroute.getAirportUp());
            //添加起点城市
            showAirroute.setFromCity(fromCity);
            //添加终点城市
            String toCity = airportDao.findCityName((int) (long) airroute.getAirportDown());
            showAirroute.setToCity(toCity);

            BeanUtil.copyProperties(airroute, showAirroute);
            showAirroutes.add(showAirroute);
        }
        //copy 到 显示在页面的 实体类中
        PageInfo<Airroute> oldPageInfo = new PageInfo<>(tbAirrouteAll);
        PageInfo<ShowAirroute> pageInfo = new PageInfo<>();
        BeanUtil.copyProperties(oldPageInfo, pageInfo);
        pageInfo.setList(showAirroutes);
        return pageInfo;
    }


    @Override
    public int insertTbAirroute(RequestAirroute requestAirroute) {
        System.out.println(requestAirroute);
        Airport fromAirport = airportDao.findTbAirportById(requestAirroute.getAirportUp());
        Airport toAirport = airportDao.findTbAirportById(requestAirroute.getAirportDown());
        Airroute airroute = new Airroute();
        BeanUtil.copyProperties(requestAirroute, airroute);
        System.out.println(fromAirport.getAirportName() + "============");
        System.out.println(toAirport.getAirportName() + "============");
        airroute.setFromAirport(fromAirport.getAirportName());
        airroute.setToAirport(toAirport.getAirportName());
        airroute.setAirrouteCreatetime(new Date());
        int i = airrouteDao.insertTbAirroute(airroute);
        return i;
    }

    @Override
    public List<Airroute> findAirrouteAll() {
        return airrouteDao.findTbAirrouteAll();
    }

    @Override
    public List<Airroute> findAirrouteByCity(Integer fromCityId, Integer toCityId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("fromCityId", fromCityId);
        map.put("toCityId", toCityId);
        List<Airroute> airrouteByCity = airrouteDao.findAirrouteByCity(map);
        return airrouteByCity;
    }

    @Override
    public int deleteAirrouteById(Integer airrouteId) {
        return airrouteDao.deleteTbAirrouteById(airrouteId);
    }
}
